package com.example.admin.SQLite;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import java.util.ArrayList;

public class SqLiteOpenHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "ContactDB";
    private static final int DATABASE_VERSION = 1;
    public static final String TABLE_USER = "User";
    public static final String TABLE_ADMIN = "admin";
    private static final String TABLE_CONTACT = "Contacts";
    public static final String LUNTAN = "LunTan";
    public static final String FEIYONG = "feiyong";
    public static final String TONGZHI = "TongZhi";
    public static final String YIJIAN = "YiJian";
    public static final String BAOMING = "BaoMin";
    private static final String TABLE_COST = "Cost";
    private static final String TABLE_USERPhone = "UserPhone";
    private static final String KEY_ID = "id";
    public static final String TABLE_SC = "sc";//收藏
    public static final String TABLE_HF = "hf";//回复

    /*
        String statu;
        String title,content,CreateData,ComName,PicAddress;*/
    public SqLiteOpenHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL("CREATE TABLE " + TABLE_SC +
                "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                + "ids INTEGER, "
                + "uids INTEGER);");

        sqLiteDatabase.execSQL("CREATE TABLE " + TABLE_HF +
                "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                + "ids INTEGER, "
                + "cont TEXT, "
                + "uids TEXT, "
                + "datas TEXT);");


        sqLiteDatabase.execSQL("CREATE TABLE " + TABLE_USERPhone +
                "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                + "ids INTEGER, "
                + "Uname TEXT, "
                + "phone TEXT);");
        sqLiteDatabase.execSQL("CREATE TABLE " + TABLE_COST +
                "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                + "Uname TEXT, "
                + "Lou TEXT, "
                + "Dan TEXT, "
                + "Men TEXT, "
                + "type TEXT, "
                + "statu TEXT, "
                + "money TEXT);");
        sqLiteDatabase.execSQL("CREATE TABLE " + TABLE_USER +
                "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                + "userName TEXT, "
                + "UserPass TEXT, "
                + "UserAddress TEXT);");
        sqLiteDatabase.execSQL("CREATE TABLE " + TABLE_ADMIN +
                "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                + "adminName TEXT, "
                + "adminPassword TEXT);");
        // create table id
        sqLiteDatabase.execSQL("CREATE TABLE " + TABLE_CONTACT +
                "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                + "title TEXT, "
                + "content TEXT, "
                + "CreateData TEXT, "
                + "ComName TEXT, "
                + "statu TEXT, "
                + "uid INTEGER, "
                + "PicAddress TEXT);");
        sqLiteDatabase.execSQL("CREATE TABLE " + LUNTAN +
                "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                + "luntanTitle TEXT, "
                + "luntanContent TEXT, "
                + "luntanTime TEXT, "
                + "luntanType TEXT, "
                + "uid INTEGER, "
                + "luntanPic TEXT);");
        sqLiteDatabase.execSQL("CREATE TABLE " + FEIYONG +
                "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                + "feiyongAmount TEXT, "
                + "feiyongStatu TEXT, "
                + "feiyongShequ TEXT, "
                + "feiyongType TEXT, "
                + "feiyongHome TEXT);");
        sqLiteDatabase.execSQL("CREATE TABLE " + TONGZHI +
                "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                + "tongzhiTitle TEXT, "
                + "tongzhiContent TEXT, "
                + "tongzhiTime TEXT);");

        sqLiteDatabase.execSQL("CREATE TABLE " + YIJIAN +
                "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                + "yijianContent TEXT, "
                + "yijianState TEXT, "
                + "yijianTime TEXT, "
                + "uid INTEGER);");
        sqLiteDatabase.execSQL("CREATE TABLE " + BAOMING +
                "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                + "title TEXT, "
                + "content TEXT, "
                + "CreateData TEXT, "
                + "Num TEXT, "
                + "imgPath TEXT);");
    }
    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACT);
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_USER);
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_COST);
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_USERPhone);
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_SC);
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_HF);
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + LUNTAN);
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + FEIYONG);
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + YIJIAN);
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + BAOMING);
        onCreate(sqLiteDatabase);
    }

    public SQLiteDatabase getSqlite() {
        return this.getWritableDatabase();
    }

    public long addData(Complaint bean) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("title", bean.title);
        values.put("content", bean.content);
        values.put("CreateData", bean.CreateData);
        values.put("ComName", bean.ComName);
        values.put("statu", bean.statu);
        values.put("uid", bean.uid);
        values.put("PicAddress", bean.PicAddress);
        long id = db.insert(TABLE_CONTACT, null, values);
        Log.e("的收费收费的方式", "addData: " + id);
        return id;
    }


    public ArrayList<Complaint> FindWhereData(String statu) {
        String selection = "statu=?"; // userName为要查询的列名
        String[] selectionArgs = {statu}; // value为要查询的值

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.query(TABLE_CONTACT, null, selection, selectionArgs, null, null, null);

        ArrayList<Complaint> arrContacts = new ArrayList<>();

        while (cursor.moveToNext()) {
            Complaint model = new Complaint();
            model.id = cursor.getInt(0);
            model.title = cursor.getString(1);
            model.content = cursor.getString(2);
            model.CreateData = cursor.getString(3);
            model.ComName = cursor.getString(4);
            model.statu = cursor.getString(5);
            model.uid = cursor.getInt(6);
            model.PicAddress = cursor.getString(7);

            arrContacts.add(model);

        }
        return arrContacts;
    }



    public void updateComs(int id, String str) {

        SQLiteDatabase database = this.getWritableDatabase();

        ContentValues cv = new ContentValues();
        cv.put("ComName", str);

        database.update(TABLE_CONTACT, cv, KEY_ID + " = " + id, null);
    }


    public void DeleteContacts(int id) {
        SQLiteDatabase database = this.getWritableDatabase();
        database.delete(TABLE_CONTACT, KEY_ID + " = ? ", new String[]{String.valueOf(id)});
    }

    public ArrayList<User> getUserList() {

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_USER, null);

        ArrayList<User> arrContacts = new ArrayList<>();

        while (cursor.moveToNext()) {
            User model = new User();
            model.id = cursor.getInt(0);
            model.userName = cursor.getString(1);
            model.UserPass = cursor.getString(2);
            model.UserAddress = cursor.getString(3);

            arrContacts.add(model);

        }
        return arrContacts;
    }


    public ArrayList<User> findWhereUsers(String id) {

        String selection = "id=?"; // userName为要查询的列名
        String[] selectionArgs = {id}; // value为要查询的值
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.query(TABLE_USER, null, selection, selectionArgs, null, null, null);

        ArrayList<User> arrContacts = new ArrayList<>();

        while (cursor.moveToNext()) {
            User model = new User();
            model.id = cursor.getInt(0);
            model.userName = cursor.getString(1);
            model.UserPass = cursor.getString(2);
            model.UserAddress = cursor.getString(3);
            arrContacts.add(model);

        }
        return arrContacts;
    }




}
